package com.tongqin.distributed;

import com.tongqin.distributed.dao.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.mybatis.spring.SqlSessionFactoryBean;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Hello world!
 */
public class MyBatisApp {
    public static void main(String[] args) {

        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        SqlSession sqlSession = null;
        try {


            inputStream = Resources.getResourceAsStream(resource);

            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory build = builder.build(inputStream);

            sqlSession = build.openSession();
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);

//            User user =mapper.selectByPrimaryKey(1);

            User user1 = new User();
            user1.setId(4);
            user1.setUsername("change");
            user1.setRealname("袁大侠");
            user1.setPassword("123456");
            user1.setCreateTime(new Date());
            user1.setSex("男");

            int insert = mapper.insert(user1);
            System.out.println(insert);
//            System.out.println(user.toString());
            sqlSession.commit();

            HashMap map = new HashMap();
            ConcurrentHashMap c = new ConcurrentHashMap();
            map.put("","");
            c.put("","");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(inputStream!= null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sqlSession != null){
                sqlSession.close();
            }
        }

        System.out.println("Hello World!");
    }

    public void ss(){
        String resource = "mybatis-config.xml";
        InputStream inputStream = null;
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            SqlSessionFactory build = builder.build(inputStream);




        } catch (IOException e) {
            e.printStackTrace();
        }

    }





}
